#include <fstream>
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <cstdlib>
#include <fcntl.h>
#include "vl_buffer.h"

using namespace VL;


void find_test() {
	vlbuffer buf;
	buf.append("1234.5675234.12312");
	auto pos = buf.find_multi({"234", "123"});
	auto pos2 = buf.find(".12");
	printf("%s: %s, idx: %hhu, begin: %zu, end: %zu\n", pos.is_found ? "found":"not found", (const char *)pos.found_mbs, pos.found_idx, pos.begin_idx, pos.end_idx);
	printf("%s: .12, begin: %zu, end: %zu\n", pos2.is_found ? "found":"not found", pos2.begin_idx, pos2.end_idx);
	buf.print();
}

void read_fd_test() {
	vlbuffer buf;
	int fd = ::open("/Users/cimzzz/Documents/CProjects/vl_buffer/.tmp/1.txt", O_RDONLY);
	printf("fd read len: %zu\n", buf.append_fd(fd, 2));
	printf("fd read len: %zu\n", buf.append_fd(fd, 2));
	printf("fd read len: %zu\n", buf.append_fd_until(fd, {"99"}, 1));
	printf("fd read len: %zu\n", buf.append_fd_until(fd, {"99"}, 1));
	printf("fd read len: %zu\n", buf.append_fd_until(fd, {"99"}, 1));
	buf.print();
	::close(fd);
}

void read_in_test() {
	vlbuffer buf;
	std::ifstream in("/Users/cimzzz/Documents/CProjects/vl_buffer/.tmp/1.txt");
	printf("fd read len: %zu\n", buf.append(in, 2));
	printf("fd read len: %zu\n", buf.append(in, 2));
	printf("fd read len: %zu\n", buf.append_until(in, {"99"}, 1));
	printf("fd read len: %zu\n", buf.append_until(in, {"99"}, 1));
	printf("fd read len: %zu\n", buf.append_until(in, {"99"}, 1));
	buf.print();
	in.close();
}


void read_test() {
	vlbuffer buf;
	buf.append("123123123566za12342145");
	vlbuffer dest;
	buf.read_until(dest, "za");
	dest.print();
	printf("=======\n");
	buf.read_until(dest, "21");
	dest.print();
	printf("%s\n", dest.c_ptr());
}

int main() {
	read_in_test();
	printf("=======\n");
	read_fd_test();
	return 0;
}